'use strict'

const SMSClient = require('@alicloud/sms-sdk')
const axios = require('axios')
const _ = require('lodash')
const Env = use('Env')
const Helpers = use('Helpers')
const { LogExternalApiRequest, LogMsg } = require('../Models')
const AliYunService = {

  // 短信相关
  async sendSMS(PhoneNumbers, TemplateCode, TemplateParam) {
    const smsClient = new SMSClient({
      accessKeyId: Env.get('ALI_ACCESS_KEY_ID'),
      secretAccessKey: Env.get('ALI_ACCESS_KEY_SECRET')
    })
    console.log('[sendSMS]', TemplateParam)
    TemplateParam = (TemplateParam) ? JSON.stringify(TemplateParam) : ''
    const params = {
      PhoneNumbers,
      SignName: Env.get('SMS_SIGN_NAME'),
      TemplateCode: TemplateCode,
      TemplateParam: TemplateParam
    }
    console.log('[sendSMS]', params)
    const result = await smsClient.sendSMS(params)
    await LogMsg.create({
      openid: PhoneNumbers,
      msgBody: JSON.stringify(params),
      response: JSON.stringify(result)
    })
    return result
  }
}

module.exports = AliYunService
